﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;
using System.Data.SqlClient;
public partial class AddProject : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && !IsCallback)
        {
            txtOperateDate.Text = DateTime.Now.ToShortDateString();
            txtOperateDate.ReadOnly = true;
            Session["D_ID"] = CMSUser.Current.UserTypeName;
            CmbDisBind();
        }
    }

    //绑定城区信息
    private void CmbDisBind()
    {
        //判断用户是否已经登录
        try
        {
            if (!CMSUser.Current.IsLogin)
            {
                Response.Redirect("~/Login.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect("~/Login.aspx");
        }
        //获取当前登录的用户
        string UserName = CMSUser.Current.Name;
        if (!UserName.Equals("Administrator"))
        {
            CmbDis.DataSourceID = "SDSDis";
            CmbDis.DataBind();
            CmbDis.ReadOnly = true;
        }
        if (UserName.Equals("Administrator"))
        {
            CmbDis.DataSourceID = "SDSDisAll";
            CmbDis.DataBind();
        }
    }

    //获取街道信息
    protected void CmbStr_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        CmbCom.Items.Clear();
        FillStrCombo(e.Parameter);
    }
    protected void FillStrCombo(string dis)
    {
        if (string.IsNullOrEmpty(dis)) return;
        SDSStr.SelectParameters[0].DefaultValue = dis;
        CmbStr.DataBind();
    }

    // 获取批文号
    protected void cmbApprovalNum_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        FillAppNumCombo(e.Parameter);
    }
    protected void FillAppNumCombo(string str)
    {
        if (string.IsNullOrEmpty(str)) return;
        SDSAppNum.SelectParameters[0].DefaultValue = str;
        cmbApprovalNum.DataBind();
    }

    //获取社区信息
    protected void cmdCom_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (e.Parameter == "xxx")
        {
            CmbCom.Items.Clear();
        }
        else
        {
            FillComCombo(e.Parameter);
        }
    }
    protected void FillComCombo(string str)
    {
        if (string.IsNullOrEmpty(str)) return;
        SDSCom.SelectParameters[0].DefaultValue = str;
        CmbCom.DataBind();
    }

    //根据批文编号读取出批准时间及批文摘要
    protected void lbMark_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            Approal ap = dc.Approal.FirstOrDefault(o => o.ID == e.Parameter);
            cmbApprovalNum.Value = e.Parameter;
            mmMark.Text = ap.Approal_Summary;
            txtApprovalDate.Text = ap.Approal_Date;

        }
    }

    //添加数据
    private void InsertPro()
    {
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            ProjectDetails pro = new ProjectDetails();
            pro.Project_ID = DateTime.Now.ToString("yyyyMMddHHmmssff");
            pro.Project_Name = txtProName.Text;
            pro.Type_ID = Convert.ToInt32(cmbType.Value.ToString());
            if (!CmbStr.Text.Equals("") && CmbCom.Text.Equals(""))
            {
                pro.Community_ID = CmbStr.Value.ToString();
            }
            else if (!CmbCom.Text.Equals("") && !CmbStr.Text.Equals(""))
            {
                pro.Community_ID = CmbCom.Value.ToString();
            }
            else
            {
                pro.Community_ID = CmbDis.Value.ToString();
            }
            pro.Project_BasicInfo = txtProBasic.Text;
            pro.Project_Address = txtProAdd.Text;
            pro.Project_Scope = txtProScope.Text;
            pro.ServeObiect = txtServerObj.Text;
            pro.ServerPersontime = txtServerPer.Text;
            pro.ContactWay = txtContact.Text;
            pro.other = txtOther.Text;
            pro.Project_Development = txtProDev.Text;
            pro.Project_CitySubsidy = txtCityFund.Text;
            pro.Project_DistrictFund = txtDisFund.Text;
            pro.Project_FundsTotal = txtTotalFunds.Text;
            pro.OperateDate = Convert.ToDateTime(txtOperateDate.Text);
            pro.ReportDate = Convert.ToDateTime(txtReportDate.Text);
            //pro.ApprovalDate = txtApprovalDate.Text.ToString();
            pro.ApprovalNumber = cmbApprovalNum.Value.ToString();
            //pro.Mark = mmMark.Text;
            dc.ProjectDetails.InsertOnSubmit(pro);
            dc.SubmitChanges();
            Frame.Global.GlobalFunctions.Alert("项目申报成功");
        }
    }

    //向数据库提交数据
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtProName.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请输入项目名称！");
        }
        else if (cmbType.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请选择项目类型！");
        }
        //else if (CmbStr.Text.Equals(""))
        //{
        //    Frame.Global.GlobalFunctions.Alert("请选择社区！");
        //}
        else if (cmbApprovalNum.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请选择批文号！");
        }
        else if (txtApprovalDate.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请选择批文号！");
        }
        else if (txtReportDate.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请输入申报时间！");
        }
        else
        {
            InsertPro();
        }

    }

    //重置页面，数据清空
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtProName.Text = "";
        cmbType.Text = "";
        CmbDis.Text = "";
        CmbStr.Text = "";
        CmbCom.Text = "";
        txtProBasic.Text = "";
        txtProAdd.Text = "";
        txtProScope.Text = "";
        txtServerObj.Text = "";
        txtServerPer.Text = "";
        txtContact.Text = "";
        txtOther.Text = "";
        txtProDev.Text = "";
        txtCityFund.Text = "";
        txtDisFund.Text = "";
        txtTotalFunds.Text = "";
        txtReportDate.Text = "";
        txtApprovalDate.Text = "";
        cmbApprovalNum.Text = "";
        mmMark.Text = "";
    }
}